<?php
/**
 * 分页
 *
 */

class Page extends Etpass_common {
	/**
	 * 起始行数
	 *
	 * @var int
	 */
    public $firstRow	;
    /**
     * 列表每页显示行数
     *
     * @var int
     */
    public $listRows	;
    /**
     * 页数跳转时要带的参数
     *
     * @var str
     */
    public $parameter  ;
    /**
     * 分页总页面数
     *
     * @var int
     */
    protected $totalPages  ;
    /**
     * 总行数
     *
     * @var int
     */
    protected $totalRows  ;
    /**
     * 当前页数
     *
     * @var unknown_type
     */
    protected $nowPage    ;
    /**
     * 分页的栏的总页数
     *
     * @var int
     */
    protected $coolPages   ;
    /**
     * 分页栏每页显示的页数
     *
     * @var int
     */
    protected $rollPage   ;
	// 分页显示定制
    //protected $config  =	array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
    /**
     * 分页显示定制
     *
     * @var unknown_type
     */
     protected $config   =   array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页');
     /**
      * 分页变量
      *
      * @var str
      */
     protected $var_page;
     protected $var_page_has_other_info = false;
     protected $url_rewrite;
     const DEFAULT_NUM=10;
     const PRE_NUM=5;
    /**
     * 构造函数
     * @access public
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     * @param array $url_rewrite 重写规则数组
     */
    public function __construct($totalRows,$listRows,$rollPage='',$var_page='',$parameter='',$url_rewrite=array()) {
    	$this->var_page = !empty($var_page)?$var_page:'p';
        $this->totalRows = $totalRows;
        $this->parameter = $parameter;
        $this->url_rewrite = $url_rewrite;
        $this->rollPage = !empty($rollPage)?$rollPage:5;
        $this->listRows = !empty($listRows)?$listRows:10;
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);

        if (strpos($_GET[$this->var_page],'_') !== false) {// || !empty($this->url_rewrite)
           $this->var_page_has_other_info = true;
           $p_sort_order = explode('_',$_GET[$this->var_page]);
           $this->nowPage  = !empty($p_sort_order[0])?$p_sort_order[0]:1;//当前页           
        }else{
           $this->nowPage  = !empty($_GET[$this->var_page])?$_GET[$this->var_page]:1;//当前页
        }
        //$this->nowPage  = !empty($_GET[$this->var_page])?$_GET[$this->var_page]:1;//当前页
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
        //print_r($this->url_rewrite);
    }
      
    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }
    
    public function get_nowPage(){
    	return $this->nowPage;
    } 
    public function get_totalPages(){
    	return $this->totalPages;
    }
    
    public function get_url(){
        $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
        $parse = parse_url($url);
        if(isset($parse['query'])) {
            parse_str($parse['query'],$params);
            unset($params[$p]);
            
            $url   =  $parse['path'].'?'.http_build_query($params);
        }
        return $url;
    }
    public function get_url_str($page=1){
    	if ($this->parameter) {
    	   $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;	
    	}else{
    		$url  =  $_SERVER['REQUEST_URI'];
    	}
        $parse = parse_url($url);
        $pathinfo = pathinfo($url);
        //print_r($pathinfo);
        $path = explode('?',$pathinfo['extension']);
        //echo $_SERVER['REQUEST_URI'];exit;
        //echo $url;exit;
        if ($path[0] == 'html') {//重写
        	if ($this->var_page_has_other_info) {
        	   $url = preg_replace("/-([0-9]*)_(.*)_(.*)\.html/","-".$page."_\$2"."_\$3".".html",$url);	
        	}else{
        		$url = preg_replace('/-[0-9]*\.html/','-'.$page.'.html',$url);
        	}
        }elseif ($this->url_rewrite) {
                $url = hotel_service::get_search_url($this->url_rewrite);
	        	$url = preg_replace("/-([0-9]*)_(.*)_(.*)\.html/","-".$page."_\$2"."_\$3".".html",$url);	
        }elseif (preg_match('/\/brand-.*-[0-9]{1,2}/i',$pathinfo['dirname'])){//品牌详情
        	     if ($page == 1) {
        	      	$url = preg_replace('/\/[0-9]*\//','/',$url);
        	      } else {
        	        $url = preg_replace('/\/[0-9]*\//','/'.$page.'/',$url);
        	      }
        }elseif (preg_match('/brand-.*-[0-9]{1,2}/i',$pathinfo['basename'])){//品牌详情
        	    if ($page == 1) {
        	      //$url = $url.'/';
        	    }else {
        	      $url = $url.$page.'/';
        	    }
        }else{
		        if(isset($parse['query']) && !empty($parse['query'])) {
		            parse_str($parse['query'],$params);
		            $p = $this->var_page;
		            unset($params[$p]);
		            $url   =  $parse['path'].'?'.http_build_query($params);
		            if (empty($params)) {
		               $url .= ''.$this->var_page.'='.$page;	
		            }else{
		               $url .= '&'.$this->var_page.'='.$page;
		            }
		        }else{
		        	$url .= '?'.$this->var_page.'='.$page;
		        }
        }
        
        return $url;
    }
    
    /**
     * 分页显示输出
     * @access public
     * @return str
     */
    public function show($isSimple = false) {
        if(0 == $this->totalRows) return '';
        $nowCoolPage      = ceil($this->nowPage/$this->rollPage);

        
        //$url = $this->get_url();
        //上下翻页字符串
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;
        $upPage = $downPage = '';
        if ($upRow>0){
            $upPage="<li class='pageback_button_select'><a href='".$this->get_url_str($upRow)."'>".$this->config['prev']."</a></li>";
        }else{
        	if ($this->totalPages>1) {
        	   $upPage="<li class='pageback_button'><a href='javascript:void(0);'>".$this->config['prev']."</a></li>";//没有上一页置灰
        	}
        }
        if ($downRow <= $this->totalPages){
            $downPage="<li class='nextpage_select'><a href='".$this->get_url_str($downRow)."' class='nextpage'>".$this->config['next']."</a></li>";
        }else{
            if ($this->totalPages>1) { 
               $downPage="<li class='nextpage'><a href='javascript:void(0);'>".$this->config['next']."</a></li>";//没有下一页置灰
            }
        }
        // 第一页 和 最后页 
        $offset = ceil($this->rollPage/2); // 如果 rollpage 是奇数则当前页前后的元素数是一样的，否则前面会比后面少一个。
        $offset_pre = $offset-1; // 当前页 在分页栏里 前面的元素数
        $offset_suff = $this->rollPage-$offset;//当前页 在分页栏里 后面的元素数。
        
        $off_pre = $this->nowPage-$offset_pre; //分页栏的第一个元素
        $off_suff = $this->nowPage+$offset_suff;//分页栏的最后一个元素
        
        $theFirst = ""; // 前n页
        $prePage = "";// 分页栏前省略号
        $nextPage = ""; // 分页栏后省略号
        $theEnd="";     //最后一页  
        
        if ($off_pre <= 0) {
        	$off_pre = 1;
        }
        if ($isSimple) {
	        if ($off_pre > 2) {
	        	$prePage = "<li>...</li>";
	        }
	        if ($off_pre > 1) {
	        	$theFirst = "<li><a href='".$this->get_url_str(1)."'>1</a></li>";//第一页
	        }
	        
        }else{
	        if ($this->totalPages <= self::DEFAULT_NUM  || ($this->totalPages > self::DEFAULT_NUM && $this->nowPage < self::DEFAULT_NUM)) {
	    	    for ($k = 1;$k<$off_pre;$k++){
	    	   	  $theFirst .= "<li><a href='".$this->get_url_str($k)."'>".$k."</a></li>";//前n页	
	    	    }
	        	if ($off_pre >= 1 && $off_pre <self::DEFAULT_NUM-$offset_pre) {
	        	   $off_suff = 10; 
	        	}
	        }else{
	    	    for ($k = 1;$k<=self::PRE_NUM ;$k++){
	    	   	  $theFirst .= "<li><a href='".$this->get_url_str($k)."'>".$k."</a></li>";//前5页	
	    	    }
	        	$prePage = "<li>...</li>";// 分页栏前省略号
	        }
        }
        
        if ($off_suff > $this->totalPages) {
        	$off_suff = $this->totalPages;
        }
        if ($off_suff<=$this->totalPages - 2) {
            $nextPage = "<li>...</li>";	
        }
        if ($off_suff<=$this->totalPages - 1) {
            $theEnd = "<li><a href='".$this->get_url_str($this->totalPages)."' >".$this->totalPages."</a></li>";//最后一页
        }
        
        // 1 2 3 4  生成分页栏
        $linkPage = "";
        for ($k = $off_pre;$k<=$off_suff ; $k++){
            if($k!=$this->nowPage){
                if($k<=$this->totalPages){
                    $linkPage .= "<li><a href='".$this->get_url_str($k)."'>".$k."</a></li>";
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= "<li><a href='javascript:void(0);'  class='select_pagelist'>".$k."</a></li>";
                }
            }
        }
        
        $pageStr = $upPage.' '.$theFirst.' '.$prePage.' '.$linkPage.' '.$nextPage.' '.$theEnd.' '.$downPage;
        return $pageStr;
    }

}
?>